self.onmessage = async function (e) {
  const data = JSON.parse(e.data);
  // 超10条前九展示后面整合成一条其他
  function handleFacilityActivitiesOver10(payload: { oLinks: any[] }) {
    const { oLinks } = payload;
    const linkMap = new Map();
    let links = [];
    const ONE = "创新中心一期";
    const TWO = "创新中心二期";
    linkMap.set(ONE, []);
    linkMap.set(TWO, []);
    // 找到第4层 把一期二期分开存
    for (let i = 0; i < oLinks.length; i++) {
      const ol = oLinks[i];
      if (ol.target.split(",").length >= 4 && ol.target.includes(ONE)) {
        linkMap.get(ONE).push(ol);
      }
      if (ol.target.split(",").length >= 4 && ol.target.includes(TWO)) {
        linkMap.get(TWO).push(ol);
      }
    }
    const hasOtherMap = new Map();
    for (const [key, value] of linkMap.entries()) {
      // 排序
      value.sort((a, b) => b.value - a.value);
      // 第10后合并为其他
      for (let i = 9; i < value.length; i++) {
        value[i].oldTarget = value[i].target;
        value[i].target = `${key},其他`;
        links.push(value[i]);
        if (!hasOtherMap.has(value[i].target)) hasOtherMap.set(value[i].target, { name: value[i].target });
      }
    }
    // 删除 nodes 被合并的项
    // nodes = [...oNodes.filter(e => !links.map(u => u.oldTarget).includes(e.name)), ...hasOtherMap.values()];
    const _nodes = [];
    const _links = oLinks.sort((a, b) => b.value - a.value);
    _links.forEach(e => {
      _nodes.push(e.source, e.target);
    });
    const uniqueNode = [...new Set(_nodes)].map(e => {
      return { name: e };
    });
    return { nodes: uniqueNode, links: _links };
  }
  const process = () => {
    return new Promise<{ nodes: any; links: any }>(resolve => {
      const result = handleFacilityActivitiesOver10(data);
      resolve(result);
    });
  };

  const result = await process();
  self.postMessage(JSON.stringify(result));
};
